//
//  KosspotTabBarView.swift
//  Kosspot
//
//  Created by 李鑫 on 2025/6/19.
//

import SwiftUI

enum Tab: String, CaseIterable {
    case home = "house"
    case sport = "sport"
    case messages = "message"
    case profile = "person"
    
    var title: String {
        switch self {
        case .home: return "housen"
        case .sport: return "sportn"
        case .messages: return "messagen"
        case .profile: return "personn"
        }
    }
}


struct KosspotTabBarView: View {
    @State private var selectedTab: Tab = .home
    @State private var tabBarHeight: CGFloat = 68
    var body: some View {
        ZStack(alignment: .bottom) {
            // 内容区
            Group {
                switch selectedTab {
                case .home: HomeView().environmentObject(CardStore())
                case .sport: SportView()
                case .messages:MessagesView()
                case .profile: ProfileView()
                }
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
            .padding(.bottom, tabBarHeight)
            Spacer()
            // 自定义TabBar
            HStack {
                Spacer()
                HStack(spacing: 0) {
                    ForEach(Tab.allCases, id: \.self) { tab in
                        TabButton(
                            tab: tab,
                            selectedTab: $selectedTab,
                            tabBarHeight: $tabBarHeight
                        )
                    }
                }
                .frame(width:Bounds.width-32,height: tabBarHeight)
                .background(Color(hex: "#191919"))
                .cornerRadius(34)
                Spacer()
            }
            .background(Color(hex: "#0A0A0A"))
            
        }
        .navigationBarBackButtonHidden(true)
    }
}

struct TabButton: View {
    let tab: Tab
    @Binding var selectedTab: Tab
    @Binding var tabBarHeight: CGFloat
    var body: some View {
        Button {
            withAnimation(nil) {
                selectedTab = tab
            }
        } label: {
            VStack(spacing: 4) {
                Image((selectedTab == tab ? tab.rawValue:tab.title))
            }
            .frame(maxWidth: .infinity, maxHeight: .infinity)
        }
    }
}


#Preview {
    KosspotTabBarView()
}
